package com.linlang.grasharepro.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.linlang.grasharepro.R;
import com.linlang.grasharepro.dao.NewMessageDB;
import com.linlang.grasharepro.dao.ShareMessageDB;
import com.linlang.grasharepro.model.NewMessageDomain;
import com.linlang.grasharepro.model.ShareMessage;
import com.linlang.grasharepro.utils.FileUtiles;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商家WIFI适配器
 * Created by pengtao on 2016/6/3.
 */
public class BiYeShareMessageAdapter extends BaseAdapter {

    private NewMessageDB newMessageDB;
    /**
     * 商家信息列表
     */
    private List<ShareMessage> list;

    public List<ShareMessage> getList() {
        return list;
    }

    public void setList(List<ShareMessage> list) {
        this.list = list;
    }

    public Context getCtx() {
        return ctx;
    }

    public void setCtx(Context ctx) {
        this.ctx = ctx;
    }


    private Context ctx;


    public BiYeShareMessageAdapter(List<ShareMessage> list, Context ctx) {
        this.list = list;
        this.ctx = ctx;
        // 实例化获取图片的类

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder hold;
        if (convertView == null) {
            hold = new Holder();
            convertView = View.inflate(ctx, R.layout.item_biye_content_message, null);
            hold.mContent = (TextView) convertView.findViewById(R.id.share_message_content);//内容
            hold.mUser = (TextView) convertView.findViewById(R.id.share_message_username);//用户名
            hold.mTime = (TextView) convertView.findViewById(R.id.share_message_time);//发布时间
            hold.mType = (ImageView) convertView.findViewById(R.id.share_message_type);//用户类型
            hold.mHead = (ImageView) convertView.findViewById(R.id.share_message_head);
            hold.reply = (ImageView) convertView.findViewById(R.id.iv_reply);
            convertView.setTag(hold);
        } else {
            hold = (Holder) convertView.getTag();
        }

        if (list != null && list.size() > 0) {
            final ShareMessage smd = list.get(position);
            hold.mUser.setText(smd.getUserName());
            hold.mTime.setText(smd.getTime());
            hold.mContent.setText(smd.getContent());

            if ("在校生".equals(smd.getType()))
                hold.mType.setImageResource(R.drawable.inschool);

            hold.mHead.setVisibility(View.VISIBLE);
            Bitmap bitmap = FileUtiles.getSmallBitmap(smd.getHeadUrl());
            hold.mHead.setImageBitmap(bitmap);

            //点击回复按钮回复留言
            hold.reply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final EditText inputServer = new EditText(ctx);
                    inputServer.setFocusable(true);

                    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
                    builder.setTitle("回复").setIcon(
                            R.drawable.icon).setView(inputServer).setNegativeButton("取消", null);
                    builder.setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    String inputName = inputServer.getText().toString();
                                    replay(smd, inputName);

//                        Toast.makeText(getApplicationContext(), inputName, Toast.LENGTH_SHORT).show();
                                }
                            });
                    builder.show();
                }
            });
        }

        // 设置默认显示的图片
//        hold.mHead.setImageResource(R.drawable.atm);


        /**
         Bitmap bit = loadImg.loadImage(hold.mHead, ConstantsModel.SHOPLISTIMGURL
         + list.get(position).getHeadUrl(), new LoadImg.ImageDownloadCallBack() {
        @Override public void onImageDownload(ImageView imageView, Bitmap bitmap) {
        // 网络交互时回调进来防止错位
        if (hold.mHead.getTag().equals(
        ConstantsModel.SHOPLISTIMGURL + list.get(position).getHeadUrl())) {
        // 设置网络下载回来图片显示
        hold.mHead.setImageBitmap(bitmap);
        }
        }
        });

         // 从本地获取的
         if (bit != null) {
         // 设置缓存图片显示
         hold.mHead.setImageBitmap(bit);
         }*/

        return convertView;
    }

    /**
     * 回复
     */
    public void replay(ShareMessage smd, String inputName) {
        //获取用户登陆信息
        SharedPreferences preferences = ctx.getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");

        String dateStr = "";
        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            dateStr = simpleDateFormat.format(date);
        } catch (Exception e) {

        }

        //新增新消息表
        NewMessageDomain newMessageDomain = new NewMessageDomain();
        newMessageDomain.setType("3");
        newMessageDomain.setFromUser(account);
        newMessageDomain.setToUser(smd.getUserName());
        newMessageDomain.setContent(inputName);
        newMessageDomain.setTime(dateStr);

        newMessageDB = new NewMessageDB(ctx);
        newMessageDB.open();
        newMessageDB.insertNewMessage(newMessageDomain);

        Toast.makeText(ctx, newMessageDomain.toString(), Toast.LENGTH_SHORT).show();
    }

    static class Holder {
        TextView mUser, mTime, mContent;
        ImageView mHead, mType, reply;
    }
}
